package model

import (
	"github.com/google/uuid"
	"gorm.io/gorm"
)

const (
	AdminRoleName = "admin"
)

type Role struct {
	ID       string
	RoleName string `gorm:"not null"`
	Enabled  bool
}

func IsAdminByRole(role string) bool {
	return AdminRoleName == role
}

func CreateRole(db *gorm.DB, r *Role) (*Role, error) {
	if r.ID == "" {
		r.ID = uuid.NewString()
	}
	err := db.Create(r).Error
	return r, err
}

func DeleteRole(db *gorm.DB, id string) error {
	return db.Where("id = ?", id).Delete(&Role{}).Error
}

func FindRoleByName(db *gorm.DB, name string) (*Role, error) {
	r := &Role{}
	err := db.Where("role_name", name).First(r).Error
	return r, err
}

func ListRoles(db *gorm.DB, filters map[string]interface{}) ([]Role, error) {
	rr := []Role{}
	err := db.Where(filters).Find(&rr).Error
	return rr, err
}

func UpdateRole(db *gorm.DB, r *Role) error {
	return db.Model(&Role{ID: r.ID}).Updates(*r).Error
}
